package com.longwang.run;

import com.longwang.entity.ResouceContent;
import com.longwang.entity.ResouceLable;
import com.longwang.service.ILinkConfigService;
import com.longwang.service.IResouceContentService;
import com.longwang.service.IResouceLableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import javax.servlet.ServletContext;

/**
 * 启动服务加载数据
 * @author 竹林听雨
 * @date 2018年9月12日 下午9:16:47
 */
@Component("startupRunner")
public class StartupRunner implements CommandLineRunner {

	@Autowired
	private ServletContext application;

    @Autowired
    private IResouceContentService iResouceContentService;

    @Autowired
    private ILinkConfigService iLinkConfigService;

    @Autowired
    private IResouceLableService iResouceLableService;


    @Override
	public void run(String... args) {

		this.loadData();  // 加载数据
	}

	/**
	 * 加载数据到applicaton缓存中
	 */
	public void loadData(){
        application.setAttribute("allList", iLinkConfigService.findAll(null, 1,1,50).getData().getList());  // 首页50个友情链接

        ResouceLable resouceLable = new ResouceLable();
        resouceLable.setIsNews(1);
        application.setAttribute("allNewsLableList", iResouceLableService.findAll(resouceLable,1,50).getData().getList());  // 首页50个热门标签

        ResouceContent resouceContent = new ResouceContent();
        resouceContent.setIsShow(1);
        resouceContent.setSiteType(1);
        resouceContent.setOrder(1);
        application.setAttribute("clicktArticleList", iResouceContentService.findAll(resouceContent,1,10).getData().getList());  // 10条点击排行文章
        resouceContent.setOrder(2);
        resouceContent.setIsNews(1);
        application.setAttribute("newsArticleList", iResouceContentService.findAll(resouceContent,1,10).getData().getList());  // 10条最新文章
        resouceContent.setIsNews(0);
        resouceContent.setIsRecommend(1);
        application.setAttribute("recommendArticleList", iResouceContentService.findAll(resouceContent,1,10).getData().getList());  // 10条推荐文章
        resouceContent.setSiteType(2);
        application.setAttribute("recommendVideoList", iResouceContentService.findAll(resouceContent,1,10).getData().getList());  // 10个推荐视频
        resouceContent.setSiteType(3);
        application.setAttribute("recommendFileList", iResouceContentService.findAll(resouceContent,1,10).getData().getList());  // 10个推荐软件

    }

}
